package onfido

import (
	"code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/common/enums"
	"code.bydev.io/cht/customer/kyc_admin_service/internal/pkg/kycprovider/dto"
)

// WorkflowStatus Workflow Run Status
// NOTE: https://documentation.onfido.com/#workflow-run-status
type WorkflowStatus string

const (
	WorkflowStatusAwaitingInput WorkflowStatus = "awaiting_input"
	WorkflowStatusProcessing    WorkflowStatus = "processing"
	WorkflowStatusAbandoned     WorkflowStatus = "abandoned"
	WorkflowStatusError         WorkflowStatus = "error"
	WorkflowStatusApproved      WorkflowStatus = "approved"
	WorkflowStatusReview        WorkflowStatus = "review"
	WorkflowStatusDeclined      WorkflowStatus = "declined"
)

func (w WorkflowStatus) IsApproved() bool {
	return w == WorkflowStatusApproved
}

// https://documentation.onfido.com/#report-status
type ReportStatus string

const (
	ReportStatusAwaitingData     ReportStatus = "awaiting_data"
	ReportStatusAwaitingApproval ReportStatus = "awaiting_approval"
	ReportStatusCancelled        ReportStatus = "cancelled"
	ReportStatusComplete         ReportStatus = "complete"
	ReportStatusWithdrawn        ReportStatus = "withdrawn"
	ReportStatusPaused           ReportStatus = "paused"
)

// ReportResult
// MORE: https://documentation.onfido.com/#report-results
type ReportResult string

const (
	ReportResultsClear       ReportResult = "clear"
	ReportResultConsider     ReportResult = "consider"
	ReportResultUnidentified ReportResult = "unidentified"
	ReportResultNull         ReportResult = ""
)

func (r ReportResult) IsClear() bool {
	return r == ReportResultsClear
}

func (r ReportResult) NonClear() bool {
	return r != ReportResultsClear
}

func (r ReportResult) IsConsider() bool {
	return r == ReportResultConsider
}

// ReportSubResult
// MORE: https://documentation.onfido.com/#sub-results-document-reports
type ReportSubResult string

const (
	ReportSubResultClear     ReportSubResult = "clear"
	ReportSubResultReject    ReportSubResult = "rejected"
	ReportSubResultSuspected ReportSubResult = "suspected"
	ReportSubResultCaution   ReportSubResult = "caution"
)

func (r ReportSubResult) IsClear() bool {
	return r == ReportSubResultClear
}

// https://documentation.onfido.com/#proof-of-address-document-source-types
type DocumentSourceType string

const (
	DocumentSourceTypePDF   DocumentSourceType = "pdf"
	DocumentSourceTypePhoto DocumentSourceType = "photo_of_screen"
	DocumentSourceTypeShot  DocumentSourceType = "screenshot"
	DocumentSourceTypePaper DocumentSourceType = "paper_document"
)

func (w WorkflowStatus) ToReviewState() dto.ReviewState {
	switch w {
	case WorkflowStatusDeclined, WorkflowStatusApproved:
		return dto.StateComplete
	case WorkflowStatusReview:
		return dto.StatePending
	case WorkflowStatusError:
		return dto.StateReset
	case WorkflowStatusAbandoned, WorkflowStatusProcessing, WorkflowStatusAwaitingInput:
		return dto.StateInit
	}
	return dto.StateInit
}

type TaskDefID string

const (
	TaskDefIDStart          TaskDefID = "start"
	TaskDefIDUploadDocument TaskDefID = "upload_document_photo"
	TaskDefIDDocumentCheck  TaskDefID = "document_check"
	TaskDefIDUploadFace     TaskDefID = "upload_face_photo"
	TaskDefIDFaceCheck      TaskDefID = "face_check_photo"
	TaskDefIDCondition      TaskDefID = "condition"
	TaskDefIDPassApplicant  TaskDefID = "pass_applicant"
	TaskDefIDPOACapture     TaskDefID = "proof_of_address_capture"
	TaskDefIDPOACheck       TaskDefID = "proof_of_address_check"
	TaskDefIDFailApplicant  TaskDefID = "fail_applicant"
)

func (t TaskDefID) IsLevel1Check() bool {
	return t == TaskDefIDDocumentCheck
}

func (t TaskDefID) IsLevel2Check() bool {
	return t == TaskDefIDPOACheck
}

const (
	RejectLabelAgeValidation          string = "AGE_VALIDATION"
	RejectLabelCompromisedDocument    string = "COMPROMISED_DOCUMENT"
	RejectLabelDataComparison         string = "DATA_COMPARISON"
	RejectLabelDataConsistency        string = "DATA_CONSISTENCY"
	RejectLabelDataValidation         string = "DATA_VALIDATION"
	RejectLabelImageIntegrity         string = "IMAGE_INTEGRITY"
	RejectLabelPoliceRecord           string = "POLICE_RECORD"
	RejectLabelVisualAuthenticity     string = "VISUAL_AUTHENTICITY"
	RejectLabelDocumentClassification string = "DOCUMENT_CLASSIFICATION"
)

type WorkflowRunAction string

const (
	WorkflowRunActionCompleted WorkflowRunAction = "workflow_run.completed"
)

func (a WorkflowRunAction) IsActionCompleted() bool {
	return a == WorkflowRunActionCompleted
}

type ResourceType string

const (
	ResourceTypeWorkflowRun ResourceType = "workflow_run"
	ResourceTypeCheck       ResourceType = "check"
	ResourceTypeReport      ResourceType = "report"
)

func (r ResourceType) IsWorkflowRun() bool {
	return r == ResourceTypeWorkflowRun
}

type OnfidoDocumentType string

const (
	// Identity Document Types
	OnfidoDocumentTypeUnknown                    OnfidoDocumentType = "unknown"                // Unknown
	OnfidoDocumentTypeIDCard                     OnfidoDocumentType = "national_identity_card" // National Identity Card
	OnfidoDocumentTypeDriver                     OnfidoDocumentType = "driving_licence"        // Driving Licence
	OnfidoDocumentTypePassport                   OnfidoDocumentType = "passport"               // Passport
	OnfidoDocumentTypeVoteID                     OnfidoDocumentType = "voter_id"               // Voter ID
	OnfidoDocumentTypeWorkPermit                 OnfidoDocumentType = "work_permit"            // Work Permit
	OnfidoDocumentTypeResidencePermie            OnfidoDocumentType = "residence_permit"       // Residence Permit
	OnfidoDocumentTypeVISA                       OnfidoDocumentType = "visa"                   // VISA
	OnfidoDocumentTypeGeneric                    OnfidoDocumentType = "generic"                // Others
	OnfidoDocumentTypePassportCard               OnfidoDocumentType = "passport_card"
	OnfidoDocumentTypeTaxID                      OnfidoDocumentType = "tax_id"
	OnfidoDocumentTypeImmigration                OnfidoDocumentType = "immigration_status_document"
	OnfidoDocumentTypeAsylum                     OnfidoDocumentType = "asylum_registration_card"
	OnfidoDocumentTypePostal                     OnfidoDocumentType = "postal_identity_card"
	OnfidoDocumentTypeSocial                     OnfidoDocumentType = "social_security_card"
	OnfidoDocumentTypeNaturalisation             OnfidoDocumentType = "certificate_of_naturalisation"
	OnfidoDocumentTypeHomeLetter                 OnfidoDocumentType = "home_office_letter"
	OnfidoDocumentTypeBirthCert                  OnfidoDocumentType = "birth_certificate"
	OnfidoDocumentTypeCitizenship                OnfidoDocumentType = "proof_of_citizenship"
	OnfidoDocumentTypeNHICard                    OnfidoDocumentType = "national_health_insurance_card"
	OnfidoDocumentTypeServiceCard                OnfidoDocumentType = "service_id_card"
	OnfidoDocumentTypeIndigenous                 OnfidoDocumentType = "indigenous_card"
	OnfidoDocumentTypeMunicipality               OnfidoDocumentType = "municipality_identity_card"
	OnfidoDocumentTypeOperators                  OnfidoDocumentType = "private_operators_card"
	OnfidoDocumentTypeVehicle                    OnfidoDocumentType = "vehicle_registration_certificate"
	OnfidoDocumentTypeProfesssionalQualification OnfidoDocumentType = "professional_qualification_card"

	// Proof of Address
	OnfidoDocumentTypeBankStatement          OnfidoDocumentType = "bank_building_society_statement" // Bank Statement/Building Society Statements
	OnfidoDocumentTypeUtilityBill            OnfidoDocumentType = "utility_bill"                    // Utility Bill
	OnfidoDocumentTypeGasBill                OnfidoDocumentType = "gas_bill"                        // Gas Bill
	OnfidoDocumentTypeCouncilTax             OnfidoDocumentType = "council_tax"                     // Council Tax
	OnfidoDocumentTypeBenefitLetters         OnfidoDocumentType = "benefit_letters"                 // Benefits Letter
	OnfidoDocumentTypeIdentityAddress        OnfidoDocumentType = "identity_document_with_address"
	OnfidoDocumentTypePension                OnfidoDocumentType = "pension_property_statement_letter"
	OnfidoDocumentTypeInsure                 OnfidoDocumentType = "insurance_statement"
	OnfidoDocumentTypeGereralLetter          OnfidoDocumentType = "general_letter"
	OnfidoDocumentTypeMobileBill             OnfidoDocumentType = "mobile_phone_bill"
	OnfidoDocumentTypeMortgage               OnfidoDocumentType = "mortgage_statement"
	OnfidoDocumentTypeElectricityBill        OnfidoDocumentType = "electricity_bill"
	OnfidoDocumentTypeInternetBill           OnfidoDocumentType = "internet_bill"
	OnfidoDocumentTypePhoneBill              OnfidoDocumentType = "phone_bill"
	OnfidoDocumentTypeWaterBill              OnfidoDocumentType = "water_bill"
	OnfidoDocumentTypeAddressCertificateBill OnfidoDocumentType = "address_certificate"
)

func (d OnfidoDocumentType) GetDocType() dto.DocType {
	switch d {
	case OnfidoDocumentTypeIDCard, OnfidoDocumentTypeVoteID, OnfidoDocumentTypeWorkPermit, OnfidoDocumentTypeVISA,
		OnfidoDocumentTypeGeneric, OnfidoDocumentTypeUnknown, OnfidoDocumentTypePassportCard, OnfidoDocumentTypeTaxID,
		OnfidoDocumentTypeImmigration, OnfidoDocumentTypeAsylum, OnfidoDocumentTypePostal, OnfidoDocumentTypeSocial,
		OnfidoDocumentTypeNaturalisation, OnfidoDocumentTypeHomeLetter, OnfidoDocumentTypeBirthCert,
		OnfidoDocumentTypeCitizenship, OnfidoDocumentTypeNHICard, OnfidoDocumentTypeServiceCard,
		OnfidoDocumentTypeIndigenous, OnfidoDocumentTypeMunicipality, OnfidoDocumentTypeOperators,
		OnfidoDocumentTypeVehicle, OnfidoDocumentTypeProfesssionalQualification:
		return dto.ID
	case OnfidoDocumentTypePassport:
		return dto.Passport
	case OnfidoDocumentTypeResidencePermie:
		return dto.LivingPermits
	case OnfidoDocumentTypeDriver:
		return dto.DrivingLicense
	case OnfidoDocumentTypeUtilityBill, OnfidoDocumentTypeBankStatement, OnfidoDocumentTypeCouncilTax,
		OnfidoDocumentTypeGasBill, OnfidoDocumentTypeBenefitLetters, OnfidoDocumentTypeIdentityAddress,
		OnfidoDocumentTypePension, OnfidoDocumentTypeInsure, OnfidoDocumentTypeGereralLetter,
		OnfidoDocumentTypeMobileBill, OnfidoDocumentTypeMortgage, OnfidoDocumentTypeElectricityBill, OnfidoDocumentTypeInternetBill,
		OnfidoDocumentTypePhoneBill, OnfidoDocumentTypeWaterBill, OnfidoDocumentTypeAddressCertificateBill:
		return dto.Bill
	}

	return dto.UndefinedDocType
}

func (d OnfidoDocumentType) ToDocumentType() dto.DocumentType {
	switch d {
	case OnfidoDocumentTypeIDCard, OnfidoDocumentTypeVoteID, OnfidoDocumentTypeWorkPermit, OnfidoDocumentTypeVISA,
		OnfidoDocumentTypeGeneric, OnfidoDocumentTypeUnknown, OnfidoDocumentTypePassportCard, OnfidoDocumentTypeTaxID,
		OnfidoDocumentTypeImmigration, OnfidoDocumentTypeAsylum, OnfidoDocumentTypePostal, OnfidoDocumentTypeSocial,
		OnfidoDocumentTypeNaturalisation, OnfidoDocumentTypeHomeLetter, OnfidoDocumentTypeBirthCert,
		OnfidoDocumentTypeCitizenship, OnfidoDocumentTypeNHICard, OnfidoDocumentTypeServiceCard,
		OnfidoDocumentTypeIndigenous, OnfidoDocumentTypeMunicipality, OnfidoDocumentTypeOperators,
		OnfidoDocumentTypeVehicle, OnfidoDocumentTypeProfesssionalQualification:
		return dto.DocumentTypeID

	case OnfidoDocumentTypePassport:
		return dto.DocumentTypePassport

	case OnfidoDocumentTypeResidencePermie:
		return dto.DocumentTypeResidence

	case OnfidoDocumentTypeDriver:
		return dto.DocumentTypeDriver

	case OnfidoDocumentTypeUtilityBill, OnfidoDocumentTypeBankStatement, OnfidoDocumentTypeCouncilTax,
		OnfidoDocumentTypeGasBill, OnfidoDocumentTypeBenefitLetters, OnfidoDocumentTypeIdentityAddress,
		OnfidoDocumentTypePension, OnfidoDocumentTypeInsure, OnfidoDocumentTypeGereralLetter,
		OnfidoDocumentTypeMobileBill, OnfidoDocumentTypeMortgage, OnfidoDocumentTypeElectricityBill, OnfidoDocumentTypeInternetBill,
		OnfidoDocumentTypePhoneBill, OnfidoDocumentTypeWaterBill, OnfidoDocumentTypeAddressCertificateBill:
		return dto.DocumentTypeBill
	}

	return dto.DocumentTypeUnknown
}

func (d OnfidoDocumentType) GetSubDocType() enums.IDSubType {
	switch d {
	case OnfidoDocumentTypeIDCard:
		return enums.IDSubType_ID_SUb_TYPE_NATIONAL_ID
	case OnfidoDocumentTypeVoteID:
		return enums.IDSubType_ID_SUb_TYPE_NATIONAL_ID
	case OnfidoDocumentTypeWorkPermit, OnfidoDocumentTypeVISA:
		return enums.IDSubType_ID_SUb_TYPE_VISA
	case OnfidoDocumentTypeResidencePermie:
		return enums.IDSubType_ID_SUb_TYPE_RESIDENT_PERMIT_ID
	case OnfidoDocumentTypePassport: // 无二级类型
		return enums.IDSubType_ID_SUB_TYPE_UNDEFINED
	case OnfidoDocumentTypeDriver:
		return enums.IDSubType_ID_SUb_TYPE_CONSULAR_ID
	case OnfidoDocumentTypeUtilityBill, OnfidoDocumentTypeBankStatement, OnfidoDocumentTypeCouncilTax, OnfidoDocumentTypeBenefitLetters:
		return enums.IDSubType_ID_SUB_TYPE_UNDEFINED
	case OnfidoDocumentTypeGeneric:
		return enums.IDSubType_ID_SUb_TYPE_OTHER_ID
	case OnfidoDocumentTypeUnknown:
		return enums.IDSubType_ID_SUB_TYPE_UNDEFINED
	}

	return enums.IDSubType_ID_SUB_TYPE_UNDEFINED
}
